#
# LuckyBot4, a python IRC bot
# (c) Copyright 2008 by Lucas van Dijk
# http://www.return1.net
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA
#
# $Id: __init__.py 52 2009-03-26 21:52:48Z luckyluke56 $
#

from gettext import gettext as _
from luckybot.luckynet.protocols.irc import Format
from luckybot.bot.plugins import Plugin
import sys

class BotActions(Plugin):

	PLUGIN_INFO = {
		'name': 'Bot actions',
		'description': 'Some simple bot actions',
		'authors': ['Lucas van Dijk'],
		'version': '1.0',
		'website': 'http://www.return1.net'
	}

	def initialize(self):
		self.register_command('quit', self.quit, help=_("Quit the bot"), args="[quit event.message]")
		self.register_command('join', self.join_channel, help=_("Join a specified channel"), args="channel")
		self.register_command('part', self.part_channel, help=_("Leave a specified channel"), args="channel")
		self.register_command('eval', self.eval_code, help=_("Run python code from the bot"), args="code")
		self.register_command('nick', self.change_nick, help=_("Change the nick of the bot"), args="new_nick")

	def quit(self, event):
		if not event.user.check_logged_in():
			event.user.send_notice(_("You do not have enough rights to use this command"))
			return

		sys.exit()

	def join_channel(self, event):
		if not event.user.check_logged_in():
			event.user.send_notice(_("You do not have enough rights to use this command"))
			return

		if len(event.message.bot_args) == 0 or not event.message.bot_args.startswith('#'):
			event.user.send_notice(_("No channel given"))
			return

		event.client.send('JOIN %s' % event.message.bot_args)

	def part_channel(self, event):
		if not event.user.check_logged_in():
			event.user.send_notice(_("You do not have enough rights to use this command"))
			return

		if len(event.message.bot_args) == 0:
			event.channel.part()
		else:
			if not event.message.bot_args.startswith('#'):
				event.user.send_notice(_("No channel given"))
				return

			event.client.send('PART %s' % event.message.bot_args)

	def eval_code(self, event):
		if not event.user.check_logged_in():
			event.user.send_notice(_("You do not have enough rights to use this command"))
			return

		if len(event.message.bot_args) == 0:
			return

		r = None

		try:
			exec event.message.bot_args
		except Exception, e:
			r = e

		if r == "" or r == None:
			event.channel.send_pm(_("No Result"))
		else:
			event.channel.send_pm(_("Result:") + " " + str(r))

	def change_nick(self, event):
		if not event.user.check_logged_in():
			event.user.send_notice(_("You do not have enough rights to use this command"))
			return

		if len(event.message.bot_args) == 0:
			return

		event.client.send('NICK %s' % event.message.bot_args)
